package cn.iocoder.devlop.module.annual.report.entuser.controller.app;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.devlop.framework.common.pojo.CommonResult;
import cn.iocoder.devlop.framework.idempotent.core.annotation.Idempotent;
import cn.iocoder.devlop.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.devlop.framework.security.config.SecurityProperties;
import cn.iocoder.devlop.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.devlop.module.annual.report.entuser.controller.vo.EntuserCreateReqVO;
import cn.iocoder.devlop.module.annual.report.entuser.controller.vo.EntuserRespVO;
import cn.iocoder.devlop.module.annual.report.entuser.controller.vo.EntuserUpdateReqVO;
import cn.iocoder.devlop.module.annual.report.entuser.convert.EntuserConvert;
import cn.iocoder.devlop.module.annual.report.entuser.dal.dataobject.EntuserDO;
import cn.iocoder.devlop.module.annual.report.entuser.service.EntuserService;
import cn.iocoder.devlop.module.annual.report.marpripinfo.controller.vo.LoginReqVO;
import cn.iocoder.devlop.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import cn.iocoder.devlop.module.system.enums.logger.LoginLogTypeEnum;
import cn.iocoder.devlop.module.system.service.auth.AdminAuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

import static cn.iocoder.devlop.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 用户信息")
@RestController
@RequestMapping("/annual/report/entuser")
@Validated
public class AppEntuserController {

    @Resource
    private EntuserService entuserService;

    @Resource
    private AdminAuthService authService;

    @Resource
    private SecurityProperties securityProperties;

    @PutMapping("/update")
    @Operation(summary = "更新联络员")
    @PermitAll
    public CommonResult<Boolean> updateEntuser(@Valid @RequestBody EntuserUpdateReqVO updateReqVO) {
        entuserService.updateEntuser(updateReqVO);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得联络员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<EntuserRespVO> getEntuser(@RequestParam("id") Long id) {
        EntuserDO entuser = entuserService.getEntuser(id);
        return success(EntuserConvert.INSTANCE.convert(entuser));
    }



    @PostMapping("/register")
    @Operation(summary = "注册联络员")
    @PermitAll
    public CommonResult<Boolean> entUserRegister(@Valid @RequestBody EntuserCreateReqVO createReqVO) {
        entuserService.createEntuser(createReqVO);
        return success(true);
    }

    @GetMapping("/sent-sms-Code")
    @Operation(summary = "发送手机验证码短信")
    @Idempotent(timeout = 60, timeUnit = TimeUnit.SECONDS, message = "一分钟内只能发送一次验证码")
    @PermitAll
    @Parameter(name = "phone", description = "手机号", required = true, example = "1024")
    public CommonResult<Boolean> sentSMSCode(@RequestParam("phone") String phone,@RequestParam("regno") String regno,@RequestParam("type") String type) {
        entuserService.sentSMSCode(phone,regno,type);
        return success(true);
    }

    @PostMapping("/login")
    @Operation(summary = "联络员登录")
    @PermitAll
    public CommonResult<AuthLoginRespVO> entUserLogin(@Valid @RequestBody LoginReqVO loginReqVO,HttpServletRequest request) {
        return success(entuserService.entUserLogin(loginReqVO));
    }

    @GetMapping("/check")
    @Operation(summary = "检查并获取联络员信息")
    @PermitAll
    @Parameter(name = "ideneffeno", description = "身份证号", required = true)
    @Parameter(name = "regno", description = "企业注册号", required = true)
    public CommonResult<EntuserRespVO> checkMarpripinfo(@RequestParam("ideneffeno") String ideneffeno,@RequestParam("regno") String regno) {
        EntuserDO entuserDO = entuserService.getEntuserByIdeneffeno(ideneffeno,regno);
        return success(EntuserConvert.INSTANCE.convert(entuserDO));
    }


    @PostMapping("/logout")
    @Operation(summary = "登出系统")
    @PermitAll
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    public CommonResult<Boolean> logout(HttpServletRequest request) {
        String token = SecurityFrameworkUtils.obtainAuthorization(request, securityProperties.getTokenHeader());
        if (StrUtil.isNotBlank(token)) {
            authService.logout(token, LoginLogTypeEnum.LOGOUT_SELF.getType());
        }
        return success(true);
    }

}
